// 006D8D80
// int mprotect(void *addr, size_t len, int prot)




function hook_open() {
    Java.perform(function (){
      
        var fopen = Module.findExportByName("libc.so","fopen")
        Interceptor.attach(fopen , {
            onEnter: function(args){
                var name = Memory.readCString(args[0])
                if(name.indexOf('.js')!=-1)
                console.log("fileName:"+name)
            //    console.log("prot:"+args[2])
            //    var file = new File('/sdcard/zzzz/test'+i+".dll", 'wb');
            //    file.write(data.readByteArray(datalen));
            //    file.flush();
            //    file.close();
            },
            onLeave: function(){
                // console.log("open onLeave");
           
            }
        })

     

        // var modules = Process.enumerateModules()
        // for (const key in modules) {
        //     var v = JSON.stringify(modules[key])
        //     if(v.indexOf('/data/')!=-1){
        //         console.log('\n')
        //         console.log(v)
        //     }
        // }
 
    })
}

function main2222() {

    var library_name = "libcocos2djs.so" // ex: libsqlite.so
    var loaded = false
    var enums = Module.enumerateSymbols("linker")
    var tt = null
    for (var i =0 ; i< enums.length ; i++ ){
        if(enums[i].name == "__dl__ZL10dlopen_extPKciPK17android_dlextinfoPKv"){
            tt = enums[i].address
        }

        
    }
    console.log("tt", tt)
    Interceptor.attach(tt,{
        onEnter: function(args){
            var library_path = Memory.readCString(args[0])
           // console.log("found library : " + library_path)
            if( library_path.includes(library_name)){
                console.log("found library : " + library_path)
                loaded = true
            }
        },
        onLeave: function(args){
            if(loaded){

        
           var jni_OnLoad = Module.findExportByName("libcocos2djs.so","JNI_OnLoad")
           var base_addr = Module.findBaseAddress("libcocos2djs.so")
           console.log("base_addr : " + base_addr)
           console.log("jni_OnLoad_symbol : " + jni_OnLoad)
         

            // 0x0150F874+1 jni_onload
            // 0x0052AA04 evalString

            var eval_addr = base_addr.add(0x0052AA04)
            console.log("eval_addr : " + eval_addr)
            var jni_onload_offset = base_addr.add(0x0150F874+1)
            console.log("jni_onload_offset : " + jni_onload_offset)


 
          
              Interceptor.attach(jni_OnLoad ,{
                onEnter: function(args){
                    // var fileName = Memory.readCString(args[4])
                    // console.log("fileName : " + fileName)
                    console.log('jni_OnLoad')
                    },
                    onLeave: function(ret){
                    console.log('jni_OnLoad ret')


   // se::ScriptEngine::evalString(se::ScriptEngine *this, const char *a2, int a3, se::Value *a4, const char *name)
                    Interceptor.attach(eval_addr ,{
                        onEnter: function(args){
                            // var fileName = Memory.readCString(args[4])
                            // if(fileName)
                            // fileName = fileName.replaceAll("/",".")
                           // console.log("fileName : " + fileName)
                            // var data = args[1]
                            // var datalen = args[2].toInt32()
                            // var file = new File('/data/data/com.ftaro.kingwar.tyhjz/files/'+fileName, 'wb')
                            // file.write(data.readByteArray(datalen))
                            // file.flush()
                            // file.close()
                         
                            },
                            onLeave: function(ret){
                                console.log('pc111 is :',this.context.pc)
                                var r12ptr = this.context.r12.readPointer()
                                console.log("r12ptr : ",r12ptr)
                        
                        }
                      })

                      var eval_addr_1 = eval_addr.add(8)
                      Interceptor.attach(eval_addr_1 ,{
                        onEnter: function(args){
                            // console.log('eval_addr_1 enter')
                            },
                            onLeave: function(ret){
                            // console.log('pc222 is :',this.context.pc)
                            // console.log('ip222 is :',this.context.r12)
                            //  var r12ptr = this.context.r12.readPointer()
                            //  console.log(r12ptr)
                            // var pppp =this.context.pc
                            // console.log(pppp)
                            // console.log(  hexdump(pppp))
          
                        }
                      })
                }
              })


                loaded = false
            }
        }
    })



}

function main3() {
    var base_addr = Module.findBaseAddress("libcocos2djs.so")

    console.log("base_addr : " + base_addr)
    
    var eval_addr = base_addr.add(0x0052AA04)
    console.log("eval_addr : " + eval_addr)
    Interceptor.attach(eval_addr ,{
        onEnter: function(args){
            // var fileName = Memory.readCString(args[4])
            // console.log("fileName : " + fileName)
            console.log('aaaaaaaaaaaaaaa')
            },
            onLeave: function(ret){

        }
        })
}
setImmediate(main2222);


// setImmediate( main1111 );

// frida -U com.hnzh.dldlwhjx.aligames -l c2ddldl

// frida -U --no-pause -f com.ftaro.kingwar.tyhjz -l c2dty

// frida -U com.ftaro.kingwar.tyhjz -l c2dty
